import { AstTreeUtils } from "arkanalyzer";
import { ts, NodePredicate } from "qvog-dsl";
import { NewExpression, VariableStatement, is, native, ObjectLiteralExpression, rule, HomeCheckHint } from "qvog-dsl";

const SEVERITY = 3;
const DOC_PATH = '';
const DESCRIPTION = 'Require consistently using either `T[]` or `Array<T>` for arrays.';
let flag = [false, false];

export default native<HomeCheckHint>()()
    .match(VariableStatement)
    .when((node: ts.VariableStatement): boolean => {
        let declarationList = node.declarationList.declarations;
        for (let declaration of declarationList) {
            if (!ts.isVariableDeclaration(declaration)) {
                continue;
            }
            let type = declaration.type;
            if (!type || !ts.isTypeReferenceNode(type)) {
                if (flag[1]) {
                    return true;
                }
                flag[0] = true;
                continue;
            }
            const typeName = type.typeName;
            let typeText = typeName.getText();
            if (typeText !== 'ReadonlyArray' && typeText !== 'Array') {
                continue;
            }
            let args = type.typeArguments;
            if (!args || args.length === 0) {
                continue;
            }
            let typePrefix = 'any';
            let arg0 = args[0];
            if (arg0.kind === ts.SyntaxKind.StringKeyword) {
                typePrefix = 'string';
            } else if (arg0.kind === ts.SyntaxKind.NumberKeyword) {
                typePrefix = 'number';
            } else if (arg0.kind === ts.SyntaxKind.SymbolKeyword) {
                typePrefix = 'symbol';
            } else if (arg0.kind === ts.SyntaxKind.ObjectKeyword) {
                typePrefix = 'object';
            } else if (ts.isTypeReferenceNode(arg0)) {
                typePrefix = arg0.typeName?.getText() ?? 'any';
            }
            if (flag[0]) {
                return true;
            }
            flag[1] = true;
        }
        return false;
    })
    .report({
        severity: SEVERITY,
        description: DESCRIPTION,
        docPath: DOC_PATH,
    });

